<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	function Settings_model()
	{
		parent::Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	
	function get_all()
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_setting_getall();") or die(mysqli_error($iconn));
		$row = mysqli_fetch_array($result);
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $row;
	}
	
	function settings_update($cpcPrice, $cpmPrice, $adminEmail, $publisherEmail, $maxTitle, $maxBodyText, $imageW, $imageH, $servingEmail, $servingEmailName, $money_unit_value, $max_title_set_style, $max_body_set_style, $show_custom_error)
	{	
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_query($iconn, 'CALL selfserving_setting_update("' . $cpcPrice . '", "' . $cpmPrice . '", "' . $adminEmail .'", "' . $publisherEmail . '", "' . $maxTitle . '", "' . $maxBodyText . '", "' . $imageW .'", "' . $imageH . '", "' . $servingEmail .'", "' . $servingEmailName . '","' . $money_unit_value . '", "' . $max_title_set_style . '", "' . $max_body_set_style . '", "' . $show_custom_error . '" );') or die(mysqli_error($iconn));
		mysqli_close($iconn);
	}
	
	function group_category_get_all()
    {
    	$data = array();
		$iconn = $this->db->conn_id;
   		mysqli_multi_query($iconn, "CALL selfserving_group_category_get_all_v1();") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$tmp = array();
			$tmp['gcat_id'] = $row['categoryid'];
			$tmp['lstSite'] = array();
			$data[] = $tmp;
		}
		$rel = array('2200'=> array(), '1650'=>array(), '1210' => array());
		foreach($rel as $key=>$it)
		{
			$tmp = array();
			foreach($data as $item)
			{
				$tmp[] = $item;
			}
			$rel[$key] = $tmp;
		}
		
			
		mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
		
		$lstSite = array();
		$lstChannel = array();
		while($row = mysqli_fetch_array($result))
		{
			if($row['parentid'] == 0)
			{
				$tmpRow = $row;
				$tmpRow['lstChannel'] = array();
				$lstSite[$row['categoryid']][] = $tmpRow;
			}
			else
			{
				$lstChannel[] = $row;
			}
		}
	
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		$numGCat = count($data);
		$numSite = count($lstSite);
		foreach($lstChannel as $channel)
		{
			$numSite = count($lstSite[$channel['categoryid']]);
			for($j = 0; $j < $numSite; $j++)
			{
				if($lstSite[$channel['categoryid']][$j]['siteid'] == $channel['siteid'])
				{
					$lstSite[$channel['categoryid']][$j]['lstChannel'][] = $channel;
					break;
				}
			}
		}
		
		for($i = 0; $i < $numGCat; $i++)
		{
			$numSite = count($lstSite[$data[$i]['gcat_id']]);
			$arrSite = array();
			for($j = 0; $j < $numSite; $j++)
			{
				if(count($lstSite[$data[$i]['gcat_id']][$j]['lstChannel']) == 0)
				{
					$lstSite[$data[$i]['gcat_id']][$j]['lstChannel'][] = $lstSite[$data[$i]['gcat_id']][$j];
				}
			}
			foreach($lstSite[$data[$i]['gcat_id']] as $aItem)
			{
				$arrSite[] = $aItem;
			}
			$data[$i]['lstSite'] = $arrSite;			
		}
		
		for($i = 0; $i < $numGCat; $i++)
		{
			foreach($data[$i]['lstSite'] as $site)
			{
				for($j = 0; $j < $numGCat; $j++)
				{
					if($site['categoryid'] == $rel[$site['price']][$j]['gcat_id'])
					{
						$rel[$site['price']][$j]['lstSite'][] = $site;
						break;
					}
				}
			}
		}
		
		$data = null;
		return $rel;
    }
 }